package com.medical.medical.controller.yhzController;

import com.medical.medical.entity.Admin;
import com.medical.medical.entity.Doctor;
import com.medical.medical.entity.Patient;
import com.medical.medical.service.AdminService;
import com.medical.medical.service.DoctorService;
import com.medical.medical.service.PatientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@Slf4j
public class LoginController {

    @RequestMapping(value = {"/","login"})
    public String welcomeLongin(){
        return "login";
    }

    @Resource
    private PatientService patientService;
    @Resource
    private DoctorService doctorService;
    @Resource
    private AdminService adminService;

    /**
     * @Description: 登录验证
     * @author: 喜杨杨
     * @date: 2022/9/14 17:40
     * @param: [phone, password, role, session, model]
     * @return: java.lang.String
     **/
    @RequestMapping("login.do")
    public String login(String phone, String password, String role,HttpSession session, Model model){
        log.info("用户登录请求......");
        //调用service方法，进行用户匹配
        Patient patient=null;
        Doctor doctor=null;
        Admin admin=null;
        //根据role判断查询哪个表中的数据
        if (role.equals("2")){//患者
            patient = patientService.login(phone,password);
            log.info(patient+"");
        }else if (role.equals("3")){//医生
            doctor = doctorService.login(phone,password);
            log.info(doctor+"");
        }else {//管理员
            admin = adminService.login(phone,password);
            log.info(admin+"");
        }

        if(null != patient){//登录成功
            //放入session
            session.setAttribute("userSession", patient);
            //重定向页面跳转
            return "wld/main";
        }else if(null != doctor){
            session.setAttribute("userSession", doctor);
            return "wld/doctorhome";
        }else if (admin != null) {
            session.setAttribute("userSession", admin);
            return "/admin/adminhome";
        }else {
            //页面跳转（login.jsp）带出提示信息--转发
            model.addAttribute("error", "用户名或密码不正确");
            return "login";
        }
    }


    /**
     * @Description: 退出登录
     * @author: 喜杨杨
     * @date: 2022/9/14 11:01
     * @param: [session]
     * @return: java.lang.String
     **/
    @RequestMapping("logout.do")
    public String logout(HttpSession session){
        session.removeAttribute("userSession");
        return "login";
    }

}
